require "watir-webdriver"
include Watir
require "rautomation"

#Open browser
test_site = 'http://gardencity.morbo.i.patch.com/'
browser = Watir::Browser.new :ff
browser.goto(test_site)
#Maximize browser
screen_width = browser.execute_script("return screen.width;")
screen_height = browser.execute_script("return screen.height;")
browser.driver.manage.window.resize_to(screen_width,screen_height)
browser.driver.manage.window.move_to(0,0)

#Post Snapshot
browser.button(:class => "dropdown-toggle icon_post-menu-button").click
browser.a(:href => "/posts/media/new").fire_event("onclick")
sleep 4
if browser.url.include?("http://gardencity.morbo.i.patch.com/login?redirect=%2Fposts%2Fmedia%2Fnew")
  #----Request to login
  browser.text_field(:name=>"email").set("shamiul.mowla@patch.com")
  browser.text_field(:name=>"password").set("password")
  browser.button(:id=>"signin").click
  #----Post Note
  #1. Pick a group
  browser.div(:class=>"post-bucket controls").a(:text=>/Pick a group/).fire_event("onclick")
  browser.select_list(:name=>"group-chooser-select").select("Sports")
  sleep 4
  browser.link(:text=>"Running").click
  Wait.until{browser.div(:class=>"btn btn-xlarge js-add-group js-group-bucket").text.include?("Running")}
  #2. Compose Note
  #2.1 Read data from source file (.xlsx file)
  require File.dirname(__FILE__) + '/Xls.rb'  #Read data from excel file
  xlFile = XLS.new(File.dirname(__FILE__) + '/data/data_file.xlsx') #grab the data file in the same directory
  myData = xlFile.getRowRecords('A1:B6', 'data_Note') #pull data records  from excel
  xlFile.close
  #2.2 Fill data to Compose Note field
  myData.each do |record|
    if record['#'] == '4'
      browser.textarea(:id, /post_short_body/).set(record['Content'])
    end
  end
  #3. Add photo
  browser.link(:text=>"Choose a file").click
  sleep 4
  win = RAutomation::Window.new(:title => /File Upload/)
  win.text_field.set ("C:\\Users\\.321\\Gardencity\\data\\009.jpg")
  win.button(:value=>"&Open").click
  #4. Click Post
  browser.button(:id=>"post_submit").click
  #5. Validate the post.
  if browser.link(:text=>"Edit this Post").exists?
    puts ("Post is submited successfully.")
  else
    puts ("Post is not submited successfully.")
  end
end